package com.ruoyi.common.utils.jdbc;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * jdbc操作工具类
 * 需要mysql-connector-java jar包才能测试
 *
 * @author Yanzy
 */
public class JdbcUtil {

    private static Logger logger = LoggerFactory.getLogger(JdbcUtil.class);

    /**
     * 测试数据库连接
     *
     * @param uri        连接地址
     * @param dbName     数据库名
     * @param userName   用户
     * @param password   密码
     * @param driverType 驱动类型
     * @return 标识
     */
    public static boolean testConn(String uri, String dbName, String userName, String password, int driverType) {
        boolean flag = false;
        String url = "jdbc:mysql://" + uri + "/" + dbName +
                "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
        Connection connection = null;
        try {
            Class.forName(getDriver(driverType));
            connection = DriverManager.getConnection(url, userName, password);
            if (null != connection) {
                flag = true;
            }
        } catch (SQLException | ClassNotFoundException e) {
            logger.error("获取JDBC连接异常: {}", e.getMessage());
        } finally {
            close(connection);
        }
        return flag;
    }

    /**
     * 注册获取数据驱动
     *
     * @param uri        数据库ip端口
     * @param dbName     数据库名
     * @param userName   用户
     * @param password   密码
     * @param driverType 驱动类型
     * @return 连接
     */
    public static Connection getConnection(String uri, String dbName, String userName, String password, int driverType) {
        String url = "jdbc:mysql://" + uri + "/" + dbName +
                "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
        return getConnection(url, userName, password, getDriver(driverType));
    }

    /**
     * 注册获取数据驱动
     *
     * @param host       数据库地址
     * @param port       数据库端口
     * @param dbName     数据库名称
     * @param userName   名称
     * @param password   密码
     * @param driverType 驱动类型
     * @return 数据库连接
     */
    public static Connection getConnection(String host, int port, String dbName, String userName, String password, int driverType) {
        String url = "jdbc:mysql://" + host + ":" + port + "/" + dbName +
                "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
        return getConnection(url, userName, password, getDriver(driverType));
    }

    /**
     * 获取数据库版本 连接驱动
     *
     * @param type 类型 5(5~5.7) 8+
     * @return 连接驱动
     */
    private static String getDriver(int type) {
        switch (type) {
            case 8:
                return "com.mysql.cj.jdbc.Driver";
            case 5:
            default:
                return "com.mysql.jdbc.Driver";
        }
    }

    /**
     * 注册获取数据驱动
     *
     * @param uri      数据库连接全地址
     * @param userName 用户名
     * @param password 密码
     * @param driver   连接驱动
     * @return 数据库连接
     */
    private static Connection getConnection(String uri, String userName, String password, String driver) {
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(uri, userName, password);
        } catch (SQLException | ClassNotFoundException e) {
            logger.error("获取JDBC连接异常: {}", e.getMessage());
        }
        return connection;
    }

    /**
     * 关闭连接资源
     *
     * @param conn 连接对象
     */
    public static void close(Connection conn) {

        try {
            if (null != conn) {
                conn.close();
            }
        } catch (SQLException e) {
            logger.error("JDBC资源关闭异常...");
        }
    }

    /**
     * 关闭连接资源
     *
     * @param statement 传输器对象
     * @param conn      连接对象
     */
    public static void close(Statement statement, Connection conn) {

        try {
            if (null != statement) {
                statement.close();
            }
            if (null != conn) {
                conn.close();
            }
        } catch (SQLException e) {
            logger.error("JDBC资源关闭异常...");
        }
    }

    /**
     * 关闭连接资源
     *
     * @param rs        结果集对象
     * @param statement 传输器对象
     * @param conn      连接对象
     */
    public static void close(ResultSet rs, Statement statement, Connection conn) {

        try {
            if (null != rs) {
                rs.close();
            }
            if (null != statement) {
                statement.close();
            }
            if (null != conn) {
                conn.close();
            }
        } catch (SQLException e) {
            logger.error("JDBC资源关闭异常...");
        }
    }
}
